
package com.tsingtree.sys.modular.system.controller;

import com.tsingtree.base.auth.context.LoginContextHolder;
import com.tsingtree.base.auth.service.AuthService;
import com.tsingtree.roses.core.base.controller.BaseController;
import com.tsingtree.roses.core.reqres.response.ResponseData;
import com.tsingtree.roses.core.reqres.response.SuccessResponseData;
import com.tsingtree.roses.core.util.ToolUtil;
import com.tsingtree.roses.kernel.model.exception.RequestEmptyException;
import com.tsingtree.sys.core.constant.Const;
import com.tsingtree.sys.modular.system.config.Config;
import com.tsingtree.sys.modular.system.model.UploadResult;
import com.tsingtree.sys.modular.system.service.FileInfoService;
import com.tsingtree.sys.modular.system.service.IRedisUtilService;
import com.tsingtree.sys.modular.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * 登录控制器
 *
 * @author jagger
 */
@Controller
public class LoginController extends BaseController {

    @Autowired
    private AuthService authService;

    @Autowired
    private SysUserService userService;

    @Autowired
    private Config config;
    @Autowired
    private IRedisUtilService redisUtilService;
    @Autowired
    private FileInfoService fileInfoService;

    /**
     * 跳转到主页
     *
     * @author jagger
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index(Model model) {

        //判断用户是否登录
        if (LoginContextHolder.getContext().hasLogin()) {
            Map<String, Object> userIndexInfo = userService.getUserIndexInfo();
            //用户信息为空，提示账号没分配角色登录不进去
            if (userIndexInfo == null) {
                model.addAttribute("account", config.isTest? Const.DEFAULT_ACCOUNT : "");
                model.addAttribute("pwd", config.isTest? Const.DEFAULT_PWD : "");
                model.addAttribute("tips", "该用户没有角色，无法登陆");
                return "/login.html";
            } else {
                model.addAllAttributes(userIndexInfo);
                return "/index.html";
            }

        } else {
            model.addAttribute("account", config.isTest? Const.DEFAULT_ACCOUNT : "");
            model.addAttribute("pwd", config.isTest? Const.DEFAULT_PWD : "");
            return "/login.html";
        }
    }
    @RequestMapping(value = "/upload", method = RequestMethod.GET)
    public String upload(Model model) {
        String version = redisUtilService.get("app:update:version");
        String msg = redisUtilService.get("app:update:msg");
        model.addAttribute("version", version);
        model.addAttribute("msg", msg);
        String iversion = redisUtilService.get("app:update:iversion");
        String imsg = redisUtilService.get("app:update:imsg");
        model.addAttribute("iversion", iversion);
        model.addAttribute("imsg", imsg);
        return "/upload.html";
    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData upload(@RequestPart("file") MultipartFile file) {
        String pwd = super.getPara("pwd").trim();
        if(!"luvvjoy2025..".equals(pwd)){
            return ResponseData.error("密码有误！");
        }
        UploadResult uploadResult = fileInfoService.uploadApk(file);
        return new SuccessResponseData(uploadResult);
    }

    @RequestMapping(value = "/updateMsg", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData updateMsg() {
        String pwd = super.getPara("pwd").trim();
        String version = super.getPara("version").trim();
        String msg = super.getPara("msg").trim();
        String iversion = super.getPara("iversion").trim();
        String imsg = super.getPara("imsg").trim();
        if(!"luvvjoy2025..".equals(pwd)){
            throw new RequestEmptyException("密码有误！");
        }
        redisUtilService.set("app:update:version", version);
        redisUtilService.set("app:update:msg", msg);
        redisUtilService.set("app:update:iversion", iversion);
        redisUtilService.set("app:update:imsg", imsg);
        return new SuccessResponseData();
    }

    /**
     * 跳转到登录页面
     *
     * @author jagger
     */
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String loginIndex(Model model) {
        if (LoginContextHolder.getContext().hasLogin()) {
            return REDIRECT + "/";
        } else {
            model.addAttribute("account", config.isTest? Const.DEFAULT_ACCOUNT : "");
            model.addAttribute("pwd", config.isTest? Const.DEFAULT_PWD : "");
            return "/login.html";
        }
    }

    /**
     * 点击登录执行的动作
     *
     * @author jagger
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData login() {

        String username = super.getPara("username");
        String password = super.getPara("password");

        if (ToolUtil.isOneEmpty(username, password)) {
            throw new RequestEmptyException("账号或密码为空！");
        }
        //登录并创建token
        String token = authService.login(username, password);

        return new SuccessResponseData(token);
    }

    /**
     * 退出登录
     *
     * @author jagger
     */
    @RequestMapping(value = "/logout")
    @ResponseBody
    public ResponseData logOut() {
        authService.logout();
        return new SuccessResponseData();
    }

}
